/*
 * EditSettings.java
 *
 * Created on 30 de Agosto de 2007, 21:12
 *
 *
 * @author Evolution Systems - Do the Evolution
 */
package bd;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


public class EditSettings extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String fileName;
	private JComboBox cmbProtocols;
	private JButton btnOk, btnTestar, btnSair;
	private JLabel  lblProtocol;
	private JTextField txtHost, txtDbName, txtUsername;
	private JPasswordField txtPassword;
	private JPanel pnl1;
	private String protocols[] = {"POSTGRESQL","MYSQL","MSSQL","INTERBASE","ACCESS"};
	private Database bd = new Database();
	
	public EditSettings(String fileName)
	{
		setTitle("Edit settings");
		setSize(200,400);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.fileName= fileName;
		
		Container ctn = getContentPane();
		GridLayout gdl1 = new GridLayout(6,1,0, 10);
		ctn.setLayout(gdl1);
		
		pnl1 = new JPanel();
		pnl1.setLayout(new GridLayout(2,1));
		lblProtocol = new JLabel("Protocol: ");
		pnl1.add(lblProtocol);
		cmbProtocols = new JComboBox(protocols);
		pnl1.add(cmbProtocols);
		ctn.add(pnl1);
		
		pnl1 = new JPanel();
		pnl1.setLayout(new GridLayout(2,1));		
		lblProtocol = new JLabel("Host: ");
		pnl1.add(lblProtocol);
		txtHost = new JTextField(20);
		pnl1.add(txtHost);
		ctn.add(pnl1);
	
		pnl1 = new JPanel();
		pnl1.setLayout(new GridLayout(2,1));	
		lblProtocol = new JLabel("Database name: ");
		pnl1.add(lblProtocol);
		txtDbName = new JTextField(20);
		pnl1.add(txtDbName);
		ctn.add(pnl1);

		pnl1 = new JPanel();
		pnl1.setLayout(new GridLayout(2,1));		
		lblProtocol = new JLabel("User name: ");
		pnl1.add(lblProtocol);
		txtUsername = new JTextField(20);
	   pnl1.add(txtUsername);
		ctn.add(pnl1);

		pnl1 = new JPanel();
		pnl1.setLayout(new GridLayout(2,1));		
		lblProtocol = new JLabel("Password: ");
		pnl1.add(lblProtocol);
		txtPassword = new JPasswordField(20);
		pnl1.add(txtPassword);		
		ctn.add(pnl1);
		
		pnl1= new JPanel();
		pnl1.setLayout(new FlowLayout());
		btnOk = new JButton("OK");
		btnOk.addActionListener(this);
		pnl1.add(btnOk);
		btnTestar = new JButton("Test");
		btnTestar.addActionListener(this);
		pnl1.add(btnTestar);
		btnSair = new JButton("Quit");
		btnSair.addActionListener(this);
		pnl1.add(btnSair);
		ctn.add(pnl1);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String error= new String();
		
		if( event.getSource() == btnOk)
		{
			error=testDb();
			if( error.equals("")){
				bd.settings.writeSettings(fileName);
				this.dispose();
			}
			else
				JOptionPane.showMessageDialog(null,"Error try conect to database\n"+error);	
		}
		else if( event.getSource() == btnTestar)
		{	
			error= testDb();
			if( error.equals(""))
				JOptionPane.showMessageDialog(null,"Conection sucessifull!");
			else
				JOptionPane.showMessageDialog(null,"Error try conect to database\n"+error);		
		}
		else{
			this.dispose();
		}
	}
	
	public String testDb()
	{
		String password = new String(txtPassword.getPassword());
		String result= "";
		String protocol = protocols[cmbProtocols.getSelectedIndex()];
		
		if(protocol.equals( "POSTGRESQL"))
			protocol= bd.POSTGRESQL;
		else if( protocol.equals( "MYSQL"))
			protocol= bd.MYSQL;
		else if( protocol.equals( "MSSQL"))     
			protocol= bd.MSSQL;
		else if( protocol.equals( "INTERBASE"))
			protocol= bd.INTERBASE;
		else 
			protocol= bd.ACCESS;
		
		bd.settings.setDatabaseName(txtDbName.getText());
		bd.settings.setDatabaseHost(txtHost.getText());
		bd.settings.setDatabaseUser(txtUsername.getText());
		bd.settings.setDatabasePassword(password);
		bd.settings.setDatabaseProtocol(protocol);	
		
		try
		{
			if( bd.connect())
				result = "";
			else{
				result = bd.ErrorMessage;
			}
		}
		finally
		{
			bd.disconnect();	
		}	
		return result;
	}
}
